package controllers

import (
	"system/models"
	"system/pkg/app"
	"system/pkg/e"
	"system/services"

	"github.com/gin-gonic/gin"
	"github.com/unknwon/com"
)

func Get_Students(c *gin.Context) {
	page := -1
	if arg := c.Query("page"); arg != "" {
		page = com.StrTo(arg).MustInt()
	}
	limit := -1
	if arg := c.Query("limit"); arg != "" {
		limit = com.StrTo(arg).MustInt()
	}
	searchName := ""
	if arg := c.Query("searchName"); arg != "" {
		searchName = arg
	}
	supplierParam := map[string]interface{}{
		"page":       page,
		"limit":      limit,
		"searchName": searchName,
	}
	err, info, total := services.GetAllStudents(supplierParam)
	if err != nil {
		app.Error(c, e.ERROR, err, err.Error())
		return
	}
	app.OK(c, map[string]interface{}{"value": info, "total": total}, "查询成功")
}

func Add_Students(c *gin.Context) {
	var studentdata models.Student
	c.ShouldBind(&studentdata)
	err := services.AddStudents(studentdata)
	if err != nil {
		app.Error(c, e.ERROR, err, err.Error())
		return
	}
	app.OK(c, map[string]interface{}{}, "添加成功")

}

func Update_Students(c *gin.Context) {
	var studentdata models.Student
	c.ShouldBind(&studentdata)
	err := services.UpdateStudents(studentdata)
	if err != nil {
		app.Error(c, e.ERROR, err, err.Error())
		return
	}
	app.OK(c, map[string]interface{}{}, "更新成功")
}

func Del_Students(c *gin.Context) {
	var studentdata models.Student
	c.ShouldBind(&studentdata)
	err := services.DelStudents(studentdata.Sno)
	if err != nil {
		app.Error(c, e.ERROR, err, err.Error())
		return
	}
	app.OK(c, map[string]interface{}{}, "删除成功")
}
